package com.yunji.hushuang.plugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 责任链模式的应用
 *
 * @author hushuang
 * @since 2019/10/06
 */
public class InterceptorChain {

    private final List<Interceptor> interceptors = new ArrayList<>();

    /**
     * 方法调用入口，返回一个包装了拦截器调用的代理对象
     */
    public Object pluginAll(Object target) {
        for (Interceptor interceptor : interceptors) {
            target = interceptor.plugin(target);
        }
        return target;
    }

    /**
     * 注册拦截器
     */
    public InterceptorChain addInterceptor(Interceptor interceptor) {
        interceptors.add(interceptor);
        return this;
    }

    /**
     * 获取所有拦截器
     */
    public List<Interceptor> getInterceptors() {
        return Collections.unmodifiableList(interceptors);
    }
}
